package com.xiaoyung.easyis.app.demo.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson2.JSONObject;
import com.xiaoyung.easyis.app.demo.domain.Person;
import com.xiaoyung.easyis.app.demo.service.PersonService;
import com.xiaoyung.easyisapisdk.common.form.SelectParam;
import com.xiaoyung.easyisapisdk.feign.api.util.Param;

public class PersonController {
	private static Logger logger = LoggerFactory.getLogger(PersonController.class);

	//private EasyisOpenApi api = EasyisOpenApi.getInstance();
	private PersonService personService = new PersonService();
	
	//map 或者responseBen 含全部,  entityBean 只含有body
	public Person add(JSONObject ctx) {// body headers query method url
		logger.debug("add 接收到参数:{}",ctx);
		//ctx.getBigDecimal(null);
		JSONObject body = ctx.getJSONObject("body");
		Person person = body.toJavaObject(Person.class);
		
		return personService.add(person);
	}
	
	public Person edit(JSONObject ctx) {// body headers query method url
		logger.debug("edit 接收到参数:{}",ctx);
		//ctx.getBigDecimal(null);
		JSONObject body = ctx.getJSONObject("body");
		Person person = body.toJavaObject(Person.class);
		
		return personService.edit(person);
	}
	
	public Integer deleteById(@Param("body.id") String id) {// body headers query method url
		logger.debug("deleteById 接收到参数:{}",id);
		return personService.deleteById(id);
	}
	
	public Integer deleteByIds(@Param("body.ids") String[] ids) {// body headers query method url
		logger.debug("deleteByIds 接收到参数:{}", ids == null ? null : Arrays.toString(ids));
		return personService.deleteByIds(ids);
	}
	
	public List<Person> select(JSONObject ctx) {// body headers query method url
		logger.debug("select 接收到参数:{}",ctx);
		//ctx.getBigDecimal(null);
		JSONObject body = ctx.getJSONObject("body");
		SelectParam param = body.toJavaObject(SelectParam.class);
		
		return personService.select(param);
	}
	public List<Person> selectByIds(@Param("body.ids") String[] ids) {// body headers query method url
		logger.debug("selectByIds 接收到参数:{}",ids == null ? null : Arrays.toString(ids));
		return personService.selectByIds(ids);
	}
	
	public Person selectById(@Param("body.id") String id) {// body headers query method url
		logger.debug("selectById 接收到参数:{}",id);
		return personService.selectById(id);
	}
	
	public Person add2(@Param("body") JSONObject body,@Param("query") Map<String,String> query,@Param("headers") Map<String,String> headers,@Param("method") String method,@Param("headers['User-Agent']") String userAgent) {// body headers query method url
		logger.debug("add3 接收到参数: body{} querr {} headers {} userAgent {}",body,query,headers,userAgent);
		Person person = body.toJavaObject(Person.class);
		return personService.add(person);
	}
	

	public Integer sum(@Param("body.num1") Integer num1, @Param(value="body.num2",defaultValue ="100") Integer num2) {
		logger.debug("sum 接收到参数: body.num1{} body.num2 {} ",num1,num2);
		return num1 + (num2==null?0:num2);
	}
	
	public Integer sum2(@Param("body.num1") Integer num1,@Param("query.num2") Integer num2) {
		logger.debug("sum2 接收到参数: body.num1 {} body.num2{}",num1,num2);
		return num1 + (num2==null?0:num2);
	}
	

}
